<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-model="showSearch" label-width="110px">
      <el-form-item label="店铺名称" prop="shopName">
        <name-input v-model="queryParams.shopName" placeholder="请输入商户简称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="签约名" prop="shopSignName">
        <name-input v-model="queryParams.shopSignName" placeholder="请输入签约名" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="法人姓名" prop="legalPersonName">
        <el-input v-model="queryParams.legalPersonName" placeholder="请输入法人姓名" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="联系人手机号" prop="contactMobile">
        <el-input v-model="queryParams.contactMobile" placeholder="请输入联系人手机号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="店铺编号" prop="shopNo">
        <el-input v-model="queryParams.shopNo" placeholder="请输入店铺编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所属平台" v-if="user.userId === 1" prop="platformNickName">
        <el-input v-model="queryParams.platformNickName" placeholder="请输入所属平台" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所属代理" v-if="user.deptId < 101" prop="agentNickName">
        <el-input v-model="queryParams.agentNickName" placeholder="请输入所属代理" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所属二级代理" v-if="user.deptId < 102" prop="childAgentNickName">
        <el-input v-model="queryParams.childAgentNickName" placeholder="请输入所属二级代理" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['ecs:yeepayWithhold:add']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list">
      <el-table-column label="代运营商编" prop="merchantNo" width="120" fixed="left" align="center" />
      <el-table-column label="店铺编号" prop="shopNo" width="150" fixed="left" align="center" />
      <el-table-column label="店铺名称" prop="shopName" fixed="left" width="200" align="center" />
      <el-table-column label="店铺签约名" prop="shopSignName" width="300" align="center" />
      <el-table-column label="所属平台" prop="platformNickName" width="200" align="center" />
      <el-table-column label="所属代理" prop="agentNickName" width="200" align="center" />
      <el-table-column label="所属二级代理" prop="childAgentNickName" width="200" align="center" />
      <el-table-column label="所属店铺" prop="shopNickName" width="200" align="center" />
      <el-table-column label="法人姓名" prop="legalPersonName" width="100" align="center" />
      <el-table-column label="联系电话" prop="contactMobile" width="100" align="center" />
      <el-table-column label="结算卡号" prop="settleCardNo" width="180" align="center" />
      <el-table-column label="创建状态" prop="createMessage" width="200" align="center" />
      <el-table-column label="授权" width="300" align="center">
        <template slot-scope="{row}">
          <div class="auth-item" v-for="auth in row.authList">
            <span v-if="auth.channelType == 'DOUYIN'">抖音</span>
            <span v-if="auth.channelType == 'MEITUAN'">美团</span>
            <span v-if="auth.channelType == 'JINGDONG'">京东</span>

            <span v-if="auth.businessType == 'HX_DZ'">核销到综</span>
            <span v-if="auth.businessType == 'HX_CY'">核销餐饮</span>
            <span v-if="auth.businessType == 'WM'">外卖</span>

            <template v-if="auth.authStatus === 'SUCCESS'">
              <el-tag type="success">成功</el-tag>
            </template>
            <template v-else-if="auth.authStatus === 'FAIL'">
              <el-tooltip :content="auth.authMessage">
                <el-tag type="danger">失败</el-tag>
              </el-tooltip>
            </template>
            <template v-else-if="auth.signUrl">
              <el-link :href="auth.signUrl" type="primary" icon="el-icon-s-check" target="_blank">授权链接</el-link>
              <el-link v-if="user.userId === 1" type="warning" icon="el-icon-refresh"
                @click="resetAuth(row, auth)">重置链接</el-link>
            </template>
            <template v-else>
              <el-tag type="info">未提交</el-tag>
            </template>
            <el-button size="mini" type="text" icon="el-icon-info" v-if="row.shopNo" @click="showAuth(row, auth)"
              v-hasPermi="['ecs:yeepayWithhold:edit']">详情</el-button>
          </div>
          <el-button size="mini" type="text" icon="el-icon-plus" v-if="row.shopNo" @click="showAuth(row)"
            v-hasPermi="['ecs:yeepayWithhold:edit']">新增</el-button>
        </template>
      </el-table-column>
      <el-table-column label="绑卡状态" prop="bindMessage" width="200" align="center">
        <template slot-scope="{row}">
          <template v-if="row.bindType === 'BIND'">
            <template v-if="row.bindStatus === 'PROCESS'">
              <el-tag type="info">绑卡处理中</el-tag>
              <el-link v-if="!!row.bindUrl" :href="row.bindUrl" type="primary" icon="el-icon-bank-card"
                v-hasPermi="['ecs:yeepayWithhold:edit']" target="_blank">绑卡链接</el-link>
            </template>
            <template v-if="row.bindStatus === 'SUCCESS'">
              <el-tag type="success">绑卡成功</el-tag>
              <el-button v-if="user.userId === 1" size="mini" type="text" icon="el-icon-close"
                v-hasPermi="['ecs:yeepayWithhold:edit']" @click="unbindCard(row)">解绑</el-button>
            </template>
            <template v-if="row.bindStatus === 'TIMEOUT'">
              <el-tag type="warning">超时未绑卡</el-tag>
              <el-button v-if="user.userId === 1" size="mini" type="text" icon="el-icon-bank-card"
                v-hasPermi="['ecs:yeepayWithhold:edit']" @click="bindCard(row)">重新绑卡</el-button>
            </template>
            <template v-if="row.bindStatus === 'FAIL'">
              <el-tooltip :content="row.bindMessage">
                <el-tag type="danger">绑卡失败</el-tag>
              </el-tooltip>
              <el-button v-if="user.userId === 1" size="mini" type="text" icon="el-icon-bank-card"
                v-hasPermi="['ecs:yeepayWithhold:edit']" @click="bindCard(row)">重新绑卡</el-button>
            </template>
          </template>
          <template v-else-if="row.bindType === 'UNBIND'">
            <template v-if="row.unbindStatus === 'PROCESS'">
              <el-tag type="info">解绑处理中</el-tag>
            </template>
            <template v-if="row.unbindStatus === 'SUCCESS'">
              <el-tag type="success">解绑成功</el-tag>
              <el-button v-if="user.userId === 1" size="mini" type="text" icon="el-icon-bank-card"
                v-hasPermi="['ecs:yeepayWithhold:edit']" @click="bindCard(row)">重新绑卡</el-button>
            </template>
            <template v-if="row.unbindStatus === 'FAIL'">
              <el-tooltip :content="row.unbindMessage">
                <el-tag type="danger">解绑失败</el-tag>
              </el-tooltip>
              <el-button v-if="user.userId === 1" size="mini" type="text" icon="el-icon-close"
                v-hasPermi="['ecs:yeepayWithhold:edit']" @click="unbindCard(row)">重新解绑</el-button>
            </template>
          </template>
          <template v-else-if="row.shopNo">
            <el-button size="mini" type="text" icon="el-icon-bank-card" @click="bindCard(row)"
              v-hasPermi="['ecs:yeepayWithhold:edit']" v-clickOnce>获取绑卡链接</el-button>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" prop="updateTime" width="200">
        <template slot-scope="{row}">{{ row.updateTime ? row.updateTime : row.createTime }}</template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="300" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button size="mini" type="text" icon="el-icon-edit" v-if="!row.shopNo" @click="showEditor(row)"
            v-hasPermi="['ecs:yeepayWithhold:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-info" v-else @click="showEditor(row)">详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-refresh" v-if="!!row.shopNo && user.userId === 1"
            @click="refresh(row)">刷新</el-button>
          <el-button size="mini" type="text" @click="feeRateOpen(row)"
            v-if="user.userId === 1 && !!row.shopNo">修改费率</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <ShopEditor ref="shopEditor" />
    <ShopAuth ref="shopAuth" />

    <el-dialog :title="feeRateTitle" :visible.sync="feeRateVisible" width="35%" append-to-body>
      <el-form ref="feeRateRef" :model="feeRateForm" label-width="110px" class="form">
        <el-form-item label="费率" prop="feeRate">
          <el-input-number :step="0.1" :min="0" :max="100" :precision="2" v-model.number="feeRateForm.feeRate"
            placeholder="请输入费率" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="feeRateSubmit">确 定</el-button>
        <el-button @click="feeRateCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getUserProfile } from '@/api/system/user'
import { getAgentUserId } from '@/api/ecs/agent'
import { listYeepayWithhold, bindYeepayWithhold, unbindYeepayWithhold, authYeepayWithhold, queryYeepayWithhold, updateYeepayWithholdFeeRate } from '@/api/ecs/yeepayWithhold'
import ShopEditor from './modules/ShopEditor.vue'
import ShopAuth from './modules/ShopAuth.vue'

export default {
  name: 'YeepayWithhold',
  components: { ShopEditor, ShopAuth },
  data() {
    return {
      loading: false,
      total: 0,
      list: [],
      showSearch: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shopName: '',
        shopSignName: '',
        legalPersonName: '',
        contactMobile: '',
        shopNo: '',
        platformNickName: '',
        agentNickName: '',
        childAgentNickName: '',
      },
      feeRateTitle: '',
      feeRateForm: { id: 0, feeRate: 0.0 },
      feeRateVisible: false,
      user: {},
    }
  },
  created() {
    getUserProfile().then(response => {
      this.user = response.data;
      if (this.user.deptId === 101) {
        getAgentUserId(this.user.userId).then(response => {
          if (response.data.agentId !== 0) {
            this.user.deptId = 102
          }
        });
      }
    });
    this.getList();
  },
  methods: {
    getList() {
      listYeepayWithhold(this.queryParams).then(res => {
        if (res.code === 200) {
          this.total = res.total
          this.list = res.rows
        }
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleAdd() {
      this.$refs.shopEditor.show().then(() => {
        this.getList()
      })
    },
    showEditor(row) {
      const disabled = !!row.shopNo
      this.$refs.shopEditor.show(row.id, disabled).then(() => {
        this.getList()
      })
    },
    showAuth(row, auth) {
      const disabled = ['PROCESSING', 'SUCCESS'].indexOf(auth?.authStatus ?? '') >= 0
      this.$refs.shopAuth.show(row.id, auth?.requestNo, disabled).then(() => {
        this.getList()
      })
    },
    resetAuth(row, auth) {
      authYeepayWithhold(row.id, auth.channelType, auth.businessType).then(() => {
        this.getList()
      })
    },
    bindCard(row) {
      bindYeepayWithhold(row.id).then(() => {
        this.getList()
      })
    },
    unbindCard(row) {
      unbindYeepayWithhold(row.id).then(() => {
        this.getList()
      })
    },
    refresh(row) {
      queryYeepayWithhold(row.id).then(() => {
        this.getList()
      })
    },
    feeRateOpen(row) {
      this.feeRateTitle = `修改【${row.shopName}】费率`
      this.feeRateForm.id = row.id
      this.feeRateForm.feeRate = row.feeRate
      this.feeRateVisible = true
    },
    feeRateSubmit() {
      updateYeepayWithholdFeeRate(this.feeRateForm).then(res => {
        if (res.code === 200) {
          this.getList()
          this.feeRateVisible = false
        }
      })
    },
    feeRateCancel() {
      this.feeRateVisible = false
      this.$refs.feeRateRef.resetFields()
    },
  }
}

</script>
<style lang="scss" scoped>
.auth-item {
  padding-bottom: 5px;
  border-bottom: 1px solid #dfe6ec;
  margin-bottom: 5px;

  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: 10px;
}
</style>
